<?php
/**
 * Created by PhpStorm.
 * User: micoya
 * Date: 2018/11/7
 * Time: 2:33 PM
 */

namespace app\admin\controller;


use app\common\base\AuthenticatedController;
use think\Controller;

class UploadController extends Controller {

    // 图片上传接口
    // WangEditor和所有图片上传共用一个接口
    public function Image() {

        $images = request()->file('image');

        $image_array = [];
        $error = '';

        foreach ($images as $image) {
            $info = $image
                ->validate(['ext'=>'jpg,png,gif,jpeg'])
                ->rule('md5')
                ->move(ROOT_PATH . 'public'. DS . 'uploads');

            if ($info) {
                $save_name = str_replace("\\", '/', $info->getSaveName());
                array_push($image_array, '/uploads/' . $save_name);
            } else {
                $error = $image->getError();
                break;
            }
        }

        if (!empty($error)) {
            return ErrorImageUpload($error);
        }

        return SuccessImageUpload($image_array);

    }

    // 文件上传接口
    public function File() {

        $files = request()->file('file');

        $file_array = [];
        $error = '';

        foreach ($files as $file) {
            $info = $file->move(ROOT_PATH . 'public'. DS . 'uploads', true, false);

            if ($info) {
                $save_name = str_replace("\\", '/', $info->getSaveName());
                array_push($file_array, '/uploads/' . $save_name);
            } else {
                $error = $file->getError();
                break;
            }
        }

        if (!empty($error)) {
            return ErrorImageUpload($error);
        }

        return SuccessImageUpload($file_array);

    }

}